package base.util.lib;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;

import base.action.lib.HttpRequestFactory;



@SuppressWarnings("all")
public class VoParam {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6861743072338016117L;

	/*------------------------------------ 此处的方法负责处理页面传递过来的参数-----------------------------------------*/

	 
	/*
	 * 将得到的参数设置到PreparedState中
	 */
		public void setSQLValues(PreparedStatement pst, Vector vlist)
				throws Exception {
			try {
				if (vlist == null)
					return;
				Object v = null;

				for (int i = 0; i < vlist.size(); ++i) {
					v = vlist.get(i);
					if (v instanceof java.util.Date) {
						java.util.Date d = (java.util.Date) v;
						v = new Timestamp(d.getTime());
					}
					pst.setObject(i + 1, v);
				}

			} catch (Exception ex) {
				ex.printStackTrace();

				throw new Exception(ex);
			}
		}
	
	/**
	 * 封装页面传过来 参数
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param cols
	 *            列名 集合
	 * @param value
	 *            参数值 集合
	 * @throws Exception
	 */
	public static void setInsertParam(HttpServletRequest request, List cols,
			Vector values) {
		try {
			Map map = request.getParameterMap();
			Set s = map.keySet();
			Iterator it = s.iterator();
			String col = "";
			while (it.hasNext()) {
				col = (String) it.next();
				int index = col.indexOf("_");
				if (index == -1) {
					continue;
				}
				String value = getValue(request.getParameter(col));
				cols.add(col.substring(index + 1, col.length()));
				if (col.indexOf("num_") == 0) {
					BigDecimal big = new BigDecimal(value);
					values.add(big);

				} else if (col.indexOf("dt_") == 0) {
					SimpleDateFormat oFormat = new SimpleDateFormat(
							"yyyy-MM-dd");
					java.util.Date date;
					try {
						date = oFormat.parse(value);
						values.add(date);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				} else {
					values.add(value);
				}
			}
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static String getValue(Object obj) {
		return obj == null ? "" : obj.toString().trim();
	}

	/**
	 * 增加、修改时设置传入字段参数操作 页面中传入字段格式为 vch_fdh、num_fdh vch 表示此字段类型为字符串 fdh 表示字段
	 * 
	 * @param cols
	 *            增加、修改字段列表
	 * @param params
	 *            增加、修改字段值列表
	 * @param dataField
	 *            时间字段
	 */
	public static void setInsertOrUpdateParams(List cols, Vector params,
			String dateField) {

		if (cols == null)
			return;
		if (params == null)
			return;

		HttpServletRequest request = HttpRequestFactory.getRequest();
		Map map = request.getParameterMap();
		Set s = map.keySet();
		Iterator it = s.iterator();
		String col = "";
		while (it.hasNext()) {
			col = (String) it.next();
			int index = col.indexOf("_");  //参数中不存在 _ 则返回
			int sel = col.indexOf("sel_"); //参数中存在 sel_ 则返回
			if (index == -1) {
				continue;
			}
			if (sel == 0) {
				continue;
			}
			String value = getValue(request.getParameter(col));
			cols.add(col.substring(index + 1, col.length()));
			if (col.indexOf("num_") == 0) {
				BigDecimal big = new BigDecimal(value);
				params.add(big);
			} else if (col.indexOf("dt_") == 0) {
				SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				java.util.Date date = null;
				try {
					date = oFormat.parse(value);
					params.add(date);
				} catch (ParseException e) {
					System.out
							.println("增加、修改时设置传入字段参数操作  日期转换异常============== "
									+ value);
					e.printStackTrace();
					return;
				}
			} else if (col.indexOf("vch_") == 0) {

				params.add(value);
			}

		}
		// dateField 不为空时，为此字段添加时间
		if (StringUtils.isNotEmpty(dateField)) {
			cols.add(dateField);
			params.add(new Date());
		}
	}

	/**
	 * 查询时设置查询字段参数操作 页面中传入查询 字段格式为 search_1_fdh search 表示查询 1 表示 =; 2 表示 like;
	 * 3表示 >= ; 4表示 <=;
	 * 
	 * @param sqlsql
	 *            查询sql语句
	 * @param params
	 *            params 查询字段值列表
	 * @param name
	 *            所取别名
	 */
	public static void setSearchParams(StringBuilder sql, Vector params,
			String name) {
		if (sql == null)
			return;
		if (params == null)
			return;

		HttpServletRequest request = HttpRequestFactory.getRequest();
		Map map = request.getParameterMap();
		Set s = map.keySet();
		Iterator it = s.iterator();
		String search_field = "";
		while (it.hasNext()) {
			search_field = (String) it.next();
			if (search_field.length() < 10) { // select_1_ 传入参数格式必须不小于10
				continue;
			}
			int index = search_field.indexOf("search_");
			if (index == 0) {
				String temp_sql = "";
				String temp_operation = ""; // 1 like 2 =
				String temp_field = "";
				String temp_param = "";
				temp_operation = search_field.substring(7, 8);
				temp_field = search_field.substring(9, search_field.length());
				temp_param = getValue(request.getParameter(search_field));
				if (temp_operation.equals("1")) { // =
					if (!temp_param.equals("")) {
						temp_sql = " and " + name + "." + temp_field + " = ? ";
						sql.append(temp_sql);
						params.add(temp_param);
					}
				} else if (temp_operation.equals("2")) { // like
					if (!temp_param.equals("")) {
						temp_sql = " and " + name + "." + temp_field
								+ " like ? ";
						sql.append(temp_sql);
						params.add("%" + temp_param + "%");
					}
				} else if (temp_operation.equals("3")) { // >=
					if (!temp_param.equals("")) {
						temp_sql = " and " + name + "." + temp_field + " >= ? ";
						sql.append(temp_sql);
						params.add(temp_param);
					}
				} else if (temp_operation.equals("4")) { // <=
					if (!temp_param.equals("")) {
						temp_sql = " and " + name + "." + temp_field + " <= ? ";
						sql.append(temp_sql);
						params.add(temp_param);
					}
				}
			}
		}
	}
}
